package com.leyou.comment.controller;


import com.leyou.comment.service.CommentService;
import com.leyou.common.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import com.leyou.comment.pojo.Comment;

@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    //查询spu评论
    @GetMapping("/spuComment")
    public ResponseEntity<PageResult<Comment>> findCommentBySpuId(@RequestParam(value = "spuId") Long spuId,
                                                                  @RequestParam(value = "score", required = false) String score,
                                                                  @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                  @RequestParam(value = "size", defaultValue = "5") Integer size) {
        PageResult<Comment> comments = commentService.findBySpuId(page, size, spuId, score);
        return ResponseEntity.ok(comments);
    }

    //查询sku评论
    @GetMapping("/skuComment")
    public ResponseEntity<PageResult<Comment>> findCommentBySkuId(@RequestParam(value = "skuId") Long skuId,
                                                                  @RequestParam(value = "score", required = false) String score,
                                                                  @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                  @RequestParam(value = "size", defaultValue = "5") Integer size) {
        PageResult<Comment> comments = commentService.findBySkuId(page, size, skuId, score);
        return ResponseEntity.ok(comments);
    }

    //修改点赞数
    @GetMapping("/updateLikeNum")
    public ResponseEntity<Void> updateLikeNum(@RequestParam("commentId") Long commentId, @RequestParam("likeNum") Integer likeNum) {
        commentService.updateLikeNum(commentId, likeNum);
        return ResponseEntity.ok().build();
    }

    //追加评论
    @GetMapping("/addContent")
    public ResponseEntity<Void> addContent(@RequestParam("commentId") Long commentId, @RequestParam("addContent") String addContent) {
        commentService.updateAddContent(commentId,addContent);
        return ResponseEntity.ok().build();
    }

    //回复评论
    @GetMapping("/updateReplyComment")
    public ResponseEntity<Void> updateReplyComment(@RequestParam("commentId") Long commentId,
                                                   @RequestParam("childContent") String childContent,
                                                   HttpServletRequest request) {
        commentService.updateReplyComment(commentId,childContent,request);
        return ResponseEntity.ok().build();
    }

    //子评论下的回复
    @GetMapping("/childReply")
    public ResponseEntity<Void> childReply(@RequestParam("commentId") Long commentId,
                                           @RequestParam("childId") Long childId,
                                           @RequestParam("replyChildContent") String replyChildContent,
                                           @RequestParam("replyChildName") String replyChildName,
                                           HttpServletRequest request) {
        commentService.childReply(commentId,childId,replyChildContent,request,replyChildName);
        return ResponseEntity.ok().build();
    }
}
